<script setup>
import { GetPopListAPI } from '@/api/mold/requisition'
const { proxy } = getCurrentInstance()
import { TreeListAPI } from '@/api/mold/edger'

// 物料类别
const props = defineProps({
  isShow: {
    type: Boolean,
    default: false,
  },
  dictId: {
    type: [String, Number],
    default: '',
  },

  title: {
    type: String,
    default: '物料',
  },
  // 是否单选
  bdb: {
    type: Boolean,
    default: false,
  },
  ids: {
    type: Array,
    default: () => [],
  },
})

const emit = defineEmits([
  'selectsubmit',
  'preservation',
  'cancel',
  'reset',
  'materialTypeChange',
])

const loading = ref(false)

// 类别树结构
const materialTypeSelsct = ref([])

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  ids: props.ids,
})

const handleSelect = ref({ materialId: '' })
// 列表
const tableData = ref([])
// 列表条数
const total = ref(0)

// 列表枚举
const column = ref([
  { label: '模具名称', prop: 'mouldName' },
  { label: '模具编码', prop: 'mouldCode' },
  { label: '模具类型', prop: 'mouldType' },
  { label: '制造厂商', prop: 'factoryName' },
  { label: '出厂时间', prop: 'factoryDate' },
])

// 勾选数据
const handleSelectionChanges = ({ ids, row }) => {
  handleSelect.value = row
}
/**
 * 树结构
 */
const dataTree = ref([])

// 勾选数据
const handleSelectionChange = ({ ids, row }) => {
  handleSelect.value = row
  // initBtn()
}
/**
 * 类别下拉树结构
 */
const getTreeList = async () => {
  try {
    const { data: res } = await TreeListAPI()
    dataTree.value = res
  } catch (err) {
    console.log(err)
  }
}
getTreeList()

// 是否禁用 保存
const d = computed(() => {
  if (props.bdb) {
    return handleSelect.value?.id ? false : true
  } else {
    return handleSelect.value.length > 0 ? false : true
  }
})
/**
 * 树结构枚举
 */
const defaultProps = {
  children: 'children',
  label: 'categoryName',
  value: 'id',
}
// 提交
const preservation = () => {
  console.log(handleSelect.value)
  emit('preservation', handleSelect.value)
}

// 重置
const reset = () => {
  for (let key in query) {
    query[key] = ''
  }
  initData()
}

/**
 * 初始化哈偶
 */
const initData = async () => {
  query.ids = props.ids?.join(',')
  query.status = 2
  const res = await GetPopListAPI(query)
  tableData.value = res.rows
  total.value = res.total
}
/**
 * 监听弹窗
 */
watch(
  () => props.isShow,
  (val) => {
    if (val) initData()
  }
)
</script>
<template>
  <!-- 查询条件 -->
  <p-dialog
    :isBtn="false"
    v-model="props.isShow"
    :title="`选择模具`"
    @close="emit('cancel')"
    width="50%"
  >
    <el-form :inline="true" label-width="100px">
      <el-form-item :label="`模具编码`">
        <el-input
          clearable
          v-model.trim="query.mouldCode"
          :placeholder="`请输入模具编码`"
        />
      </el-form-item>
      <el-form-item :label="`模具名称`">
        <el-input
          clearable
          v-model.trim="query.mouldName"
          :placeholder="`请输入模具名称`"
        />
      </el-form-item>
      <el-form-item :label="`模具类型`">
        <el-tree-select
          v-model="query.mouldType"
          :props="defaultProps"
          :data="dataTree"
          :render-after-expand="false"
          style="width: 240px"
        />
      </el-form-item>
    </el-form>

    <el-divider />
    <p-table
      keyId="id"
      :isRadio="props.bdb"
      :isSel="!props.bdb"
      :column="column"
      :total="total"
      v-model:page="query.pageNum"
      v-model:limit="query.pageSize"
      @pagination="initData"
      stripe
      border
      :data="tableData"
      highlight-current-row
      @change="handleSelectionChange"
    >
      <template #columnCell="{ column, record }">
        <p-producer
          :data="record.auxiliaryAttribute"
          v-if="column.prop == 'fzsx'"
        />
      </template>
    </p-table>

    <!-- 按钮 -->
    <div>
      <el-row class="Btn">
        <el-button type="primary" @click="initData">查询列表</el-button>
        <el-button type="info" @click="reset">重置</el-button>
        <el-button type="success" @click="preservation" :disabled="d"
          >保存</el-button
        >
        <el-button type="info" @click="emit('cancel')">取消</el-button>
      </el-row>
    </div>
  </p-dialog>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
</style>
